#include "FlowManager.h"
#include <string.h>
#include <unistd.h>
#include <thread>
using namespace std;

void read_thread(int arg) {
    int flow_id = arg;
    char data[1024] = { 0 };
    int i = 0;
    while (1)
    {
        snprintf(data, 1024, "%d", i++);
        int ret = WriteDataToBufferFlow(flow_id, data, strlen(data) + 1);
        if (ret < 0)
        {
            printf("write data error\n");
        }
        usleep(33 * 1000);
    }
}

void write_thread(int arg) {
    int flow_id = arg;
    char data[1024] = { 0 };
    int post = -1;
    int ret = 0;
    int id = 0;
    int last_id = -1;
    while (1)
    {
        ret = ReadDataFromBufferFlow(flow_id, data, 1024, &post);
        if (ret > 0)
        {
            int id = atoi(data);
            printf("read ret = %d, id:%d, post = %d\n", ret, id, post);
            if (last_id != -1 && id != last_id + 1)
            {
                printf("error: lost data\n");
            }
            last_id = id;
        }
        usleep(33 * 1000);
    }
}

int main(int argc, char *argv[])
{
    int flow_id = CreateBufferFlowId(1024, 32);
    thread read_th(read_thread, flow_id);
    thread write_th(write_thread, flow_id);
    write_th.join();
    read_th.join();
    FreeBufferFlowId(flow_id);

    return 0;
}